/*
 * @Author: liyongxuan
 * @Date: 2018/2/28
 * @Last Modified by: liyongxuan
 * @Last Modified time: 2018/2/28
 * @description: logo上传
 */
<template>
    <div>
        <div>
            <div v-if="!item.itemImg  && type != 'personal'">
                <img src="../../../static/images/logo.png" @click="uploadLogo()"/>
            </div>
            <img v-else-if="type != 'personal'" :src="showLogo" style="cursor: pointer" @click="uploadLogo()" :width="width" height="115" />
            <img v-else :src="showLogo" style="cursor: pointer" @click="uploadLogo()" :width="width" height="100" />
            <span v-if="!item.itemImg" style="display: block; padding-top:5px;color:#666">点击修改图片</span>
        </div>
        <el-dialog :visible.sync="dialogVisible" class="center-width">
            <span>
                <el-upload
                    :data="param"
                    class="avatar-uploader"
                    :action="uploadUrl"
                    :headers='header'
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" width="300">
                    <i v-else class="el-icon-plus el-avatar-uploader-icon iconSize"></i>
                </el-upload>
            </span>
            <span>
            仅支持PNG、JPG、BMP图片文件文件小于1M，尺寸180*90
            </span>
            <span slot="footer" class="dialog-footer" style="text-align: center">
                <el-button type="primary"  @click="submitUpload()">确 定</el-button>
                <el-button @click="dialogVisible = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script type="text/ecmascript-6">
export default {
    data() {
        return {
            dialogVisible: false,
            imageUrl: '',
            uploadUrl: this.baseURL + '/file/',
            param: {
                businessId: this.item.itemId,
                fileType: '10063'
            },
            header: {
                'X-Token': this.$store.state.user.token
            },
            imgId: ''
        }
    },
    computed: {
        showLogo() {
            if (this.type === 'personal' && !this.item.itemImg) {
                return require('../../../static/images/null-picture.png')
            } else {
                return this.baseURL + '/file/' + this.item.itemImg + '?X-Token=' + this.$store.state.user.token
            }
        },
        width() {
            if (this.type === 'personal') {
                return '100';
            } else {
                return '180';
            }
        }
    },
    props: {
        item: {
            type: Object,
            default: {}
        },
        // personal 个人中心的头像
        type: {
            type: String,
            default: ''
        }
    },
    methods: {
        //确定
        submitUpload() {
            this.dialogVisible = false;
            this.$emit('getId',this.imgId)
        },
        uploadLogo() {
            this.dialogVisible = true
            this.imageUrl = ''
        },
        beforeAvatarUpload(file) {
            let isJPG = false;
            if (file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/bmp') {
                isJPG = true;
            }
            const isLt1M = file.size / 1024 / 1024 < 1;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 PNG、JPG、BMP 格式!');
            }
            if (!isLt1M) {
                this.$message.error('上传头像图片大小不能超过 1MB!');
            }
            return isJPG && isLt1M;
        },
      //上传成功返回
        handleAvatarSuccess(res) {
            this.imgId = res.data.id;
            this.imageUrl = this.baseURL + '/file/' + res.data.id + '?X-Token=' + this.$store.state.user.token
        },
    }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.ca-image-upload {
    font-size: 60px;
    color: #909090;
    cursor: pointer;
}
.iconSize{
  font-size: 36px;
  margin-bottom: 48px;
}
.center-width {
  width: 66.66%;
  margin: auto
}
</style>
